package com.liuguang.testassert.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: LoggingAspect
 * @Description：
 * @Author: wanghr
 * Date: 3:48 PM 2020/6/5
 * Version: 1.0
 */
@Aspect
@Component
public class LoggingAspect {

    private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);

    @Pointcut("execution(public * com.liuguang.testassert..*Controller.*(..))")
    public void controllerPointcut() {
    }

    @Before("controllerPointcut()")
    public void before(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String requestURI = request.getRequestURI();
        logger.info("请求URI：{}",requestURI);

        String name = joinPoint.getSignature().getName();
        logger.info("请求方法名：{}",name);

//        Object[] args = joinPoint.getArgs();
//        for (int i = 0; i < args.length; i++) {
//            logger.info("切入点的参数：{}",String.valueOf(args[i]));
//        }
    }
}
